package com.yulebaby.callsenter.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.CustomerBean;

import java.util.ArrayList;

/**
 * 打电话的对话框
 */
public class CallDialog extends Dialog implements View.OnClickListener {
    private CustomerBean bean;
    private ArrayList<String> list;
    private Context context;


    private View mRv_phonenum, mRv_phonenum1, mRv_phonenum2;
    private TextView mTv_phonenum, mTv_phonenum1, mTv_phonenum2, mTv_recent, mTv_recent1, mTv_recent2;
    private View mV_phonenum2line;
    private View mView;
    private ImageView mIv_close;
    private selectNumListener listener;


    private void bindViews() {

        mRv_phonenum = mView.findViewById(R.id.rv_phonenum);
        mRv_phonenum1 = mView.findViewById(R.id.rv_phonenum1);
        mRv_phonenum2 = mView.findViewById(R.id.rv_phonenum2);
        /**
         * 电话号码
         */
        mTv_phonenum = (TextView) mRv_phonenum.findViewById(R.id.tv_phonenum);
        mTv_phonenum1 = (TextView) mRv_phonenum1.findViewById(R.id.tv_phonenum);
        mTv_phonenum2 = (TextView) mRv_phonenum2.findViewById(R.id.tv_phonenum);
        /**
         * 近期通话
         */
        mTv_recent = (TextView) mRv_phonenum.findViewById(R.id.tv_recent);
        mTv_recent1 = (TextView) mRv_phonenum1.findViewById(R.id.tv_recent);
        mTv_recent2 = (TextView) mRv_phonenum2.findViewById(R.id.tv_recent);

        mV_phonenum2line = mView.findViewById(R.id.v_phonenum2line);
        mIv_close = (ImageView) mView.findViewById(R.id.iv_close);
        /**
         * 点击
         */
        mRv_phonenum.setOnClickListener(this);
        mRv_phonenum1.setOnClickListener(this);
        mRv_phonenum2.setOnClickListener(this);
        mIv_close.setOnClickListener(this);
    }

    public CallDialog(Context context, ArrayList<String> list, CustomerBean customerBean) {
        super(context, R.style.NoTitleDialogStyle);
        this.context = context;
        this.list = list;
        this.bean = customerBean;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mView = LayoutInflater.from(context).inflate(R.layout.dialog_select_phonenum, null);
        setContentView(mView);
        bindViews();
        initData();

    }

    /**
     * 对要显示的数据进行初始化
     */
    private void initData() {
        switch (list.size()) {
            case 3:
                mRv_phonenum2.setVisibility(View.VISIBLE);
                mV_phonenum2line.setVisibility(View.VISIBLE);
                //显示号码2
                mTv_phonenum2.setText(list.get(2));
                mTv_phonenum1.setText(list.get(1));
                mTv_phonenum.setText(list.get(0));
                checkRecent();
                break;
            case 2:
                mRv_phonenum2.setVisibility(View.INVISIBLE);
                mV_phonenum2line.setVisibility(View.INVISIBLE);
                //显示号码1
                mTv_phonenum1.setText(list.get(1));
                checkRecent();
            case 1:
                mTv_phonenum.setText(list.get(0));
            default:
                break;
        }
    }

    /**
     * 检查那个号码是最近通话的号码
     */
    private void checkRecent() {
        showRecent(0);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals(bean.getLast_phone() == null ? "" : bean.getLast_phone())) {
                showRecent(i);
                break;
            }
        }
    }

    /**
     * 哪个显示最近通话
     *
     * @param whichshow
     */
    public void showRecent(int whichshow) {
        switch (whichshow) {
            case 2:
                mTv_recent.setVisibility(View.INVISIBLE);
                mTv_recent1.setVisibility(View.INVISIBLE);
                mTv_recent2.setVisibility(View.VISIBLE);
                break;
            case 1:
                mTv_recent.setVisibility(View.INVISIBLE);
                mTv_recent1.setVisibility(View.VISIBLE);
                mTv_recent2.setVisibility(View.INVISIBLE);
                break;
            case 0:
                mTv_recent.setVisibility(View.VISIBLE);
                mTv_recent1.setVisibility(View.INVISIBLE);
                mTv_recent2.setVisibility(View.INVISIBLE);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rv_phonenum:
                if (listener != null) {
                    listener.selectNum(list.get(0));
                }
                dismiss();
                break;
            case R.id.rv_phonenum1:
                if (listener != null) {
                    listener.selectNum(list.get(1));
                }
                dismiss();
                break;
            case R.id.rv_phonenum2:
                if (listener != null) {
                    listener.selectNum(list.get(2));
                }
                dismiss();
                break;
            case R.id.iv_close:
                dismiss();
                break;
        }
    }

    /**
     * 设置监听器
     *
     * @param listener
     */
    public void setNumListener(selectNumListener listener) {
        this.listener = listener;
    }


    public interface selectNumListener {
        void selectNum(String num);
    }
}
